package com.ch.demo.junit;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class TestRunner {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, InstantiationException {
        Class<Demo> demoClass = Demo.class;
        Demo demo = demoClass.newInstance();
        // 反射获取所有的公有方法，不含参数
        Method[] methods = demoClass.getMethods();
        List<Method> list = new ArrayList();
        for (Method method : methods) {
            if (method.getAnnotation(Before.class) != null){
                list.add(method);
            }
            if (method.getAnnotation(After.class) != null){
                list.add(method);
            }
            if (method.getAnnotation(Test.class) != null){
                list.add(method);
            }
        }

        for (Method o : list) {
            if (o.getName() == "before"){
                Object o1 = o.invoke(demo);
                System.out.println(o1);
            }
            if (o.getName().startsWith("test") == true){
                Object o1 = o.invoke(demo);
                System.out.println(o1);
            }
            if (o.getName() == "after"){
                 Object o1 = o.invoke(demo);
                System.out.println(o1);
            }
        }
    }
}
